/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modeles.beans;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import modeles.Rayon;
import modeles.servicesClient.ServicesRayons;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Fred
 */
@ManagedBean(name = "listeRayons")
@ViewScoped
public class ListeRayonsBean implements Serializable {

    private ServicesRayons services;
    private List<Rayon> rayons;
    private List<Rayon> rayonsTries;

    /**
     * Creates a new instance of ListeRayonsBean
     */
    public ListeRayonsBean() {
        this.services = new ServicesRayons();
    }

    public List<Rayon> getRayons() {
        if (rayons == null) {
            rayons = services.getRayons();
        }
        return rayons;
    }

    public List<Rayon> getRayonsTries() {
        return rayonsTries;
    }

    public void setRayonsTries(List<Rayon> rayonsTries) {
        this.rayonsTries = rayonsTries;
    }

    public void supprimerRayon(int id) {
        rayons = null;
        Integer i = services.remove(Integer.toString(id));
        if (i != null && i == 512) {
            ajouterMessage("Ce rayon ne peut être supprimé pour des raisons d'intégrité de la base de données", FacesMessage.SEVERITY_ERROR);
        }
    }

    public void modifierRayon(RowEditEvent event) {
        Rayon rayon = (Rayon) event.getObject();
        services.modifierRayon(rayon, Integer.toString(rayon.getId()));
    }

    private void ajouterMessage(String msg, FacesMessage.Severity severity) {
        FacesMessage message = new FacesMessage(msg);
        if (severity != null) {
            message.setSeverity(severity);
        }
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}
